import {getAllCategories} from "@/api/categories";
import {getAllPages} from "@/api/page";
import {getPostList} from "@/api/post";
import {getAllTags} from "@/api/tag";
import {getInfo} from "@/api/system";

export const state = () => ({
  info: {},
  menus: [],
  pages: [],
  tags: [],
  newPosts: []
})

export const mutations = {
  'UPDATE_GLOBAL_INFO'(state, {info, menus, pages, tags, newPosts}) {
    state.info = info
    state.menus = menus
    state.pages = pages
    state.tags = tags
    state.newPosts = newPosts
  }
}

export const actions = {
  async nuxtServerInit ({ commit }) {
    try {
      const info = await this.$axios.$request(getInfo())
      const menus =  await this.$axios.$request(getAllCategories())
      const pages = await this.$axios.$request(getAllPages())
      const tags = await this.$axios.$request(getAllTags())
      const newPosts = await this.$axios.$request(getPostList({
        page: 1,
        per_page: 10,
        order: 'desc',
        orderby: 'date'
      }))
      const res = {
        info,
        menus,
        pages,
        tags,
        newPosts
      }
      commit('UPDATE_GLOBAL_INFO', res)
      return Promise.resolve(res)
    }
    catch (e) {
      return Promise.reject(error)
    }
  }
}

// export const actions = {
//   async nuxtServerInit({ commit, state }, { app, req }) {
//     const { data: menu } = await this.$axios.$get(`${process.env.baseUrl}/wp-json/xm-blog/v1/menu`)
//
//     const result = {
//       menu: menu.mainMenu
//     }
//
//
//   }
// }
